package org.jeecgframework.web.service.impl;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.web.service.DataintegritycheckServiceI;
import org.jeecgframework.web.system.pojo.base.DataintegritycheckEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sun.star.util.Date;

@Service("dataintegritycheckService")
@Transactional
public class DataintegritycheckServiceImpl extends CommonServiceImpl implements DataintegritycheckServiceI {

	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((DataintegritycheckEntity)entity);
 	}
 	
 	public <T> Serializable save(T entity) {
 		Serializable t = super.save(entity);
 		//执行新增操作配置的sql增强
 		this.doAddSql((DataintegritycheckEntity)entity);
 		return t;
 	}
 	
 	public <T> void saveOrUpdate(T entity) {
 		super.saveOrUpdate(entity);
 		//执行更新操作配置的sql增强
 		this.doUpdateSql((DataintegritycheckEntity)entity);
 	}
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @param id
	 * @return
	 */
 	public boolean doAddSql(DataintegritycheckEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @param id
	 * @return
	 */
 	public boolean doUpdateSql(DataintegritycheckEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @param id
	 * @return
	 */
 	public boolean doDelSql(DataintegritycheckEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,DataintegritycheckEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{checkcomment }",String.valueOf(t.getCheckcomment ()));
 		sql  = sql.replace("#{checkcode}",String.valueOf(t.getCheckcode()));
 		sql  = sql.replace("#{useable}",String.valueOf(t.getUseable()));
 		sql  = sql.replace("#{checksql}",String.valueOf(t.getChecksql()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}

	@Override
	public List<Map<String, Object>> executeSql() {
		List<Map<String, Object>> li = this.findForJdbc("select * from dataintegritycheck as d where d.useable =1 order by d.checkcode");
		List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> map : li) {
			List<Object> result = new ArrayList<Object>();
			//执行各个数据sql
			List<Map<String, Object>> e = this.findForJdbc(map.get("checksql").toString());
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("checkcomment", map.get("checkcomment"));
			m.put("checkcode", map.get("checkcode"));
			//处理表格标题
			if (e.size() > 0) {
				Map<String, Object> title = e.get(0);
				List<String> titleList = new ArrayList<String>();
				for (Map.Entry entry  : title.entrySet()) {
//					Map<String, Object> mtitle = new HashMap<String, Object>();
//					Object key = entry.getKey();
//					mtitle.put("title", key);
					titleList.add(entry.getKey().toString());
				}
				List<Object> rowData = new ArrayList<Object>();
				for (int i = 0;i < e.size();i++) {
					List<String> cellData = new ArrayList<String>();
					for (String tit : titleList) {
						cellData.add(e.get(i).get(tit) != null ? e.get(i).get(tit).toString() : "");
					}
					rowData.add(cellData);
				}
				result.add(rowData);
				m.put("titleList", titleList);
				//处理表格内容
				m.put("result", result);
				m.put("isShow", 1);
			}else{
				m.put("isShow", 0);
				m.put("today",DateUtils.formatDate());
			}
			results.add(m);
		}
		return results;
	}
}